//
//  BKuserMyFavorEditingCell.m
//  bestkeep
//
//  Created by windCould on 16/12/5.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKuserMyFavorEditingCell.h"
#import "BKMyCommon.h"
@interface BKuserMyFavorEditingCell ()

@property (nonatomic,strong)UIImageView *goodsImgV;
@property (nonatomic,strong)UILabel * goodsDescLab;
@property (nonatomic,strong)UILabel * goodsPriceLab;
@property (nonatomic,strong)UILabel * goodsUselessLab;
@property (nonatomic,strong)UIView * bottomLineView;
@property (nonatomic,strong)UIButton * editBtn;
@property (nonatomic,strong)UIView * uselessView;
@property (nonatomic,strong)UILabel * goodsDetailLab;
@property (nonatomic,strong)UILabel * marketLab;
@property (nonatomic,strong)UILabel * marketPricelab;


@property (nonatomic,strong)UIControl * touchCotrol;


@end


@implementation BKuserMyFavorEditingCell

+(BKuserMyFavorEditingCell *)cellWithTableview:(UITableView *)tableview
{
    BKuserMyFavorEditingCell * cell = [tableview dequeueReusableCellWithIdentifier:BKuserMyFavorEditingCellId];
    if (!cell) {
        
        cell = [[BKuserMyFavorEditingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:BKuserMyFavorEditingCellId];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setUpViews];
    }
    
    return  self;
}

#pragma mark =========================Function===============================

-(void)isSelectFavorGoods
{
    if (self.editBtn.isSelected) {
        
        [self.editBtn setSelected:NO];
    } else {
    
        [self.editBtn setSelected:YES];
    }
    
    if ([self.myDelegate respondsToSelector:@selector(bkUserMyFavorEditingCell:clickCellIndex:deleteOrNo:)]) {
        
        [self.myDelegate bkUserMyFavorEditingCell:self clickCellIndex:self.indexRow deleteOrNo:self.editBtn.isSelected];
    }
    
}

-(void)setFavorGoodsModel:(BKUserMyFavorGoodsModel *)favorGoodsModel
{

    _favorGoodsModel = favorGoodsModel;
    
    self.goodsDescLab.text = _favorGoodsModel.goodsSpecialDescribe;
    self.goodsDetailLab.text = _favorGoodsModel.goods_name;
    if (_goodsDescLab.text.length == 0) {
        
        [_goodsDetailLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(_goodsImgV.mas_top);
            make.left.equalTo(self.goodsImgV.mas_right).offset(10);
            make.right.equalTo(self.contentView.mas_right).offset(-ApplicationBackGauge);
        }];
        _goodsDetailLab.textColor = HexColor(0x404040);
        _goodsDetailLab.font = [UIFont systemFontOfSize:14];
    } else {
    
        [_goodsDetailLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(_goodsDescLab.mas_bottom).offset(4);
            make.left.equalTo(self.goodsImgV.mas_right).offset(10);
            make.right.equalTo(self.contentView.mas_right).offset(-ApplicationBackGauge);
        }];
        _goodsDetailLab.textColor = HexColor(0x888888);
        _goodsDetailLab.font = [UIFont systemFontOfSize:12.0];
    }

    self.marketPricelab.text = [NSString stringWithFormat:@"¥%.2f", [_favorGoodsModel.market_price floatValue]];
    [self contentCrossedWithContentLab:self.marketPricelab];
    self.goodsPriceLab.text = [NSString stringWithFormat:@"¥%.2f",[_favorGoodsModel.goodsPrice floatValue]];
    [_goodsImgV load:_favorGoodsModel.goods_img_big placeholderImage:[UIImage imageNamed:@"BK_My_pic_newGoods"]];
    
    self.editBtn.selected =  _favorGoodsModel.isSelect ? YES : NO;
    
    if ([_favorGoodsModel.uselessFlag integerValue] == 0) {
        
        self.goodsUselessLab.hidden = YES;
        self.uselessView.hidden = YES;
        self.goodsPriceLab.textColor = RGBCOLOR(230, 62, 46,1);
        _goodsDescLab.textColor = HexColor(0x404040);
        _marketLab.textColor = RGBCOLOR(102, 102, 102,1);
        _marketPricelab.textColor = RGBCOLOR(102, 102, 102,1);
        _goodsImgV.alpha = 1;
    }else{
        
        self.goodsPriceLab.textColor = RGBCOLOR(136, 136, 136,1);
        self.goodsDetailLab.textColor = RGBCOLOR(136, 136, 136,1);
        self.goodsDescLab.textColor = RGBCOLOR(136, 136, 136,1);
        _marketLab.textColor = RGBCOLOR(136, 136, 136,1);
        _marketPricelab.textColor = RGBCOLOR(136, 136, 136,1);
        self.goodsUselessLab.hidden = NO;
        self.uselessView.hidden = NO;
        _goodsImgV.alpha = 0.5;
    }

}

#pragma mark =========================布局===================================
-(void)setUpViews
{
    
    _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _editBtn.selected = NO;

    [_editBtn setBackgroundImage:[UIImage imageNamed:@"BK_my_addressUnSelected"] forState:UIControlStateNormal];
    [_editBtn setBackgroundImage:[UIImage imageNamed:@"BK_my_addressDefaultSelected"] forState:UIControlStateSelected];
    [_editBtn addTarget:self action:@selector(isSelectFavorGoods) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_editBtn];
    
    _goodsImgV = [[UIImageView alloc] initWithFrame:CGRectZero];
   // _goodsImgV.layer.borderWidth = 0.3;
   // _goodsImgV.layer.borderColor = [UIColor grayColor].CGColor;
    _goodsImgV.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_goodsImgV];
    
    _uselessView = [UIView new];
    _uselessView.backgroundColor = RGBCOLOR(170, 170, 170,1);
//    _uselessView.alpha = 0.7;
    _uselessView.hidden = YES;
    [self.contentView addSubview:_uselessView];
    [self.contentView sendSubviewToBack:_uselessView];
    _goodsUselessLab = [UILabel new];
    _goodsUselessLab.backgroundColor = RGBCOLOR(170, 170, 170,1);
    _goodsUselessLab.text = @"失效";
    _goodsUselessLab.hidden = YES;
    _goodsUselessLab.textAlignment = NSTextAlignmentCenter;
    _goodsUselessLab.textColor = [UIColor whiteColor];
    _goodsUselessLab.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_goodsUselessLab];
    //"xxx//商品短描述",
    _goodsDescLab = [[UILabel alloc] initWithFrame:CGRectZero];
    _goodsDescLab.numberOfLines = 1;
    _goodsDescLab.backgroundColor = [UIColor whiteColor];
    _goodsDescLab.textColor = HexColor(0x404040);
    _goodsDescLab.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_goodsDescLab];
    //xxx商品价格（会员价+糖赋"
    _goodsPriceLab =  [[UILabel alloc]initWithFrame:CGRectZero];
    _goodsPriceLab.backgroundColor = [UIColor whiteColor];
    _goodsPriceLab.textColor = RGBCOLOR(230, 62, 46,1);
    _goodsPriceLab.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:_goodsPriceLab];
    
    _bottomLineView = [UIView new];
    _bottomLineView.backgroundColor = HexColor(0xDADADA);
    [self.contentView addSubview:_bottomLineView];
    [self layoutViews];
    //"xxx商品名称"
    _goodsDetailLab = [[UILabel alloc] initWithFrame:CGRectMake(120, 35, SCREENWIDTH - 165, 40)];
    _goodsDetailLab.numberOfLines = 2;
    _goodsDetailLab.backgroundColor = [UIColor clearColor];
    _goodsDetailLab.textColor = HexColor(0x888888);
    _goodsDetailLab.font = [UIFont systemFontOfSize:13.0];
    [self.contentView addSubview:_goodsDetailLab];
    
    _marketLab = [UILabel new];
    _marketLab.text = @"市场价：";
    _marketLab.textColor = RGBCOLOR(102, 102, 102,1);
    _marketLab.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:_marketLab];
    //xxx商品市场价格
    _marketPricelab = [UILabel new];
    _marketPricelab.font = [UIFont systemFontOfSize:12];
    _marketPricelab.textColor = RGBCOLOR(102, 102, 102,1);
    [self.contentView addSubview:_marketPricelab];
    
    _touchCotrol = [[UIControl alloc]init];
    [_touchCotrol addTarget:self action:@selector(isSelectFavorGoods) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_touchCotrol];
    
    [self layoutViews];
}


-(void)layoutViews
{
    [_touchCotrol mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.contentView.mas_top).offset(0);
        make.left.mas_equalTo(self.contentView.mas_left).offset(0);
        make.right.mas_equalTo(self.contentView.mas_right).offset(0);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0);
    }];
    
    [_editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.left.mas_equalTo(self.contentView.mas_left).offset( ApplicationBackGauge);
        make.size.mas_equalTo(CGSizeMake(20,20));
    }];
    
    
    [_goodsImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_editBtn.mas_right).offset(10);
        make.top.mas_equalTo(self.contentView.mas_top).offset(20);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-20);
        make.width.mas_equalTo(self.goodsImgV.mas_height);
    }];
    
    [_uselessView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_goodsImgV.mas_left);
        make.right.equalTo(_goodsImgV.mas_right);
        make.bottom.equalTo(_goodsImgV.mas_bottom);
        make.top.equalTo(_goodsImgV.mas_top);
    }];
    
    [_goodsUselessLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_goodsImgV.mas_left);
        make.right.equalTo(_goodsImgV.mas_right);
        make.bottom.equalTo(_goodsImgV.mas_bottom);
        make.height.equalTo(@20);
    }];
    
    [_goodsDescLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.goodsImgV.mas_right).offset(10);
        make.top.equalTo(_goodsImgV.mas_top).offset(0);
        make.right.equalTo(self.contentView.mas_right).offset(-ApplicationBackGauge);
    }];
    
    [_goodsDetailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_goodsDescLab.mas_bottom).offset(4);
        make.left.equalTo(self.goodsImgV.mas_right).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-ApplicationBackGauge);
        make.height.equalTo(@(25));
    }];
    
    [_goodsPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-35);
        make.left.equalTo(self.goodsImgV.mas_right).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-ApplicationBackGauge);
    }];
    
    [_marketLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-17);
        make.left.equalTo(self.goodsImgV.mas_right).offset(10);
        make.width.equalTo(@50);
    }];
    
    
    [_marketPricelab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-17);
        make.left.equalTo(self.marketLab.mas_right);
        make.right.equalTo(self.contentView.mas_right).offset(-ApplicationBackGauge);
    }];
    
    [_bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.contentView.mas_left).offset(0);
        make.right.mas_equalTo(self.contentView.mas_right).offset(0);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0);
        make.height.mas_equalTo(@(.5f));
    }];
}

-(void)contentCrossedWithContentLab:(UILabel *)contentLab
{
    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc]initWithString:contentLab.text];
    [attributeStr  addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, contentLab.text.length)];
    contentLab.attributedText = attributeStr;
}



@end
